package com.bluesky.drcp.business.service;

import java.util.List;

import com.bluesky.drcp.business.Function;
import com.bluesky.drcp.persistence.dao.FunctionAuthorizationDao;

public class FunctionAuthorizationService {
	private FunctionAuthorizationDao functionAuthorizationDao;

	private UserRoleService userRoleService;

	private FunctionService functionService;

	public FunctionAuthorizationDao getFunctionAuthorizationDao() {
		return functionAuthorizationDao;
	}

	public void setFunctionAuthorizationDao(
			FunctionAuthorizationDao functionAuthorizationDao) {
		this.functionAuthorizationDao = functionAuthorizationDao;
	}

	public boolean isUserAuthorized(String userId, String functionId) {
		if (userRoleService.isAdmin(userId))
			return true;

		return functionAuthorizationDao.isUserAuthorized(userId, functionId);

	}

	public boolean isUserAuthorizedByPath(String userId, String functionPath) {
		try {
			Function baseFunction = functionService.getByPath(functionPath);
			return functionAuthorizationDao.isUserAuthorized(userId,
					baseFunction.getId());
		} catch (Exception ex) {
			return false;
		}
	}
	
	public boolean isRoleAuthorized(String roleId, String functionId){
		return functionAuthorizationDao.isRoleAuthorized(roleId, functionId);
	}

	public List getAuthorizedFunctions(String roleId){
		return functionAuthorizationDao.getAuthorizedFunctions(roleId);
	}
	
	public void setAuthorizedFunctions(String roleId, List functionIds){
		functionAuthorizationDao.setAuthorizedFunctions(roleId, functionIds);
	}

	public FunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public UserRoleService getUserRoleService() {
		return userRoleService;
	}

	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

}
